package controller

import (
	"github.com/gin-gonic/gin"
	"guliedu/middleware"
	"net/http"
)

func Login(ctx *gin.Context) {
	m := make(map[string]interface{})
	login := &loginInfo{}
	ctx.ShouldBind(&login)

	member, _ := ucenterMemberService.Select(login.Username)
	if member.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    20000,
			"message": "用户名或密码错误！",
		})
		return
	}

	if member.IsAdmin != 2 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    20000,
			"message": "您没有权限登入！",
		})
		return
	}

	token, _ := middleware.GetToken(login.Username)
	m["token"] = token
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": m,
	})
}

func Info(ctx *gin.Context) {
	data := make(map[string]interface{})
	//tokenHeader := strings.Split(ctx.Request.Header.Get("Authorization"), " ")[1]
	//userToken, _ := middleware.AuthToken(tokenHeader)
	data["roles"] = []string{"admin"}
	data["name"] = "admin"
	data["avatar"] = "http://qpyccvmoa.hd-bkt.clouddn.com/u%3D4103818371%2C2434047403%26fm%3D26%26gp%3D0.jpg"
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": data,
	})
}
